/**   
 * Copyright © 2018 eSunny Info. Tech Ltd. All rights reserved.
 * 
 * @Package: com.demo.jackson.bean 
 * @author: chenliang   
 * @date: 2018年5月19日 上午9:16:52 
 */
package com.demo.jackson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

/**
 * @author chenliang
 *
 */
public class JacksonDemo {

	public static void main(String[] s) throws IOException {
		java2Json();
		map2Json();
		list2Json();
		System.out.println();
		json2Java();
	}

	/**
	 * java对象转json
	 * 
	 * @Description:TODO
	 * @author:chenliang
	 * @time:2018年5月19日 上午9:27:25
	 */
	public static void java2Json() throws IOException {

		ObjectMapper mapper = new ObjectMapper();

		Person person = new Person();
		person.setName("morris");
		person.setAge("22");
		person.setSex("男");

		String jsonString = mapper.writeValueAsString(person);
		System.out.println("java2Json----------------");
		System.out.println(jsonString);
		// 也可以输出
		// JsonGenerator generator =
		// mapper.getJsonFactory().createJsonGenerator(System.out,JsonEncoding.UTF8);
		// generator.writeObject(jsonString);
	}

	/**
	 * map2Json
	 * 
	 * @Description:TODO
	 * @author:chenliang
	 * @throws IOException
	 * @throws JsonMappingException
	 * @throws JsonGenerationException
	 * @time:2018年5月19日 上午9:29:00
	 */
	private static void map2Json() throws JsonGenerationException,
			JsonMappingException, IOException {
		Map<String, String> map = new HashMap<String, String>();

		map.put("name", "morris");
		map.put("age", "22");
		map.put("address", "hk");

		ObjectMapper mapper = new ObjectMapper();
		String jsonString = mapper.writeValueAsString(map);

		System.out.println("map2Json----------------");
		System.out.println(jsonString);
	}

	/**
	 * list2Json
	 * 
	 * @Description:TODO
	 * @author:chenliang
	 * @time:2018年5月19日 上午9:32:55
	 */
	public static void list2Json() throws IOException {

		List<String> list = new ArrayList<String>();

		list.add("name");
		list.add("age");
		list.add("address");
		list.add("sex");

		ObjectMapper mapper = new ObjectMapper();

		System.out.println("list2Json----------------");
		// 直接将集合转换为json字符串
		System.out.println(mapper.writeValueAsString(list));
	}

	/**
	 * 统一调用 json2Java
	 * 
	 * @Description:TODO
	 * @author:chenliang
	 * @time:2018年5月19日 上午10:12:50
	 */
	public static void json2Java() {
		String json = "{\"age\":2,\"sex\":\"男\",\"name\":\"silion\"}";
		String jsonmap = "{\"silion\":{\"age\":2,\"sex\":\"男\",\"name\":\"silion\"}}";
		String jsonlist = "[{\"age\":2,\"sex\":\"男\",\"name\":\"silion\"},{\"age\":2,\"sex\":\"男\",\"name\":\"silion\"}]";

		/**
		 * ObjectMapper支持从byte[]、File、InputStream、字符串等数据的JSON反序列化。
		 */

		// JSON转Class
		Person person = json2Class(json, Person.class);
		System.out.println("JSON转Class : " + person);

		// JSON转Map
		Map<String, Person> map = json2Map(jsonmap);
		System.out.println("JSON转Map : " + map);

		// JSON转List
		List<Person> person2 = json2List(jsonlist);
		System.out.println("JSON转List2 : " + person2);

	}

	/**
	 * // JSON转Class Person person = json2Class(json, Person.class);
	 * System.out.println("JSON转Class : " + user);
	 * 
	 * @Description:TODO
	 * @author:chenliang
	 * @time:2018年5月19日 上午10:03:23
	 */
	private static <T> T json2Class(String json, Class<T> c) {
		ObjectMapper mapper = new ObjectMapper();
		T t = null;
		try {
			t = mapper.readValue(json, c);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return t;
	}

	/**
	 * // JSON转Map Map<String, User> map = json2Map(jsonmap);
	 * System.out.println("JSON转Map : " + map);
	 * 
	 * @Description:TODO
	 * @author:chenliang
	 * @time:2018年5月19日 上午10:05:52
	 */
	private static Map<String, Person> json2Map(String json) {
		ObjectMapper mapper = new ObjectMapper();
		Map<String, Person> map = null;
		try {
			map = mapper.readValue(json,
					new TypeReference<Map<String, Person>>() {
					});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * // JSON转List2 List<User> users2 = json2List2(jsonlist); // JSON转List2 :
	 * [User{name='silion', age=0, birthday=Wed Oct 01 00:00:00 GMT+08:00 2014,
	 * email='silion@qq.com'}, User{name='silion', age=0, birthday=Thu Jan 01
	 * 00:00:00 GMT+08:00 2015, email='silion@qq.com'}]
	 * System.out.println("JSON转List2 : " + users2);
	 * 
	 * @Description:TODO
	 * @author:chenliang
	 * @time:2018年5月19日 上午10:08:06
	 */
	private static List<Person> json2List(String jsonlist) {
		ObjectMapper mapper = new ObjectMapper();
		List<Person> users = null;
		try {
			users = mapper.readValue(jsonlist,
					new TypeReference<List<Person>>() {
					});
		} catch (IOException e) {
			e.printStackTrace();
		}
		return users;
	}

}
